﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameBase;

namespace MilitaryPluginInterface
{
    public abstract class AIBrain : InvisibleGameEntity
    {
        protected List<Team> _teams;
        protected Military _brainOwner;

        public AIBrain(Military brainOwner)
        {
            _brainOwner = brainOwner;
            _teams = Team.getAllTeams();
        }

        public override void update(GameTime gameTime)
        {
            if (!_brainOwner.isDead())
            {
                if (_brainOwner.IsAutoControl)
                {
                    Military enemyInMyRange = findNearestEnemyInMyRangeForAttack();
                    if (enemyInMyRange != null)
                    {
                        //_brainOwner.idle();
                        _brainOwner.attack(enemyInMyRange);
                        return;
                    }


                    Factory enemyFactory = findFactoryForAttack();
                    if (enemyFactory != null)
                    {
                        if (enemyFactory.TeamOwner.isAllMilitariesDead())
                        {
                            //_brainOwner.idle();
                            _brainOwner.attack(enemyFactory);
                        }
                        else
                        {
                            Military enemyMilitary = findEnemyForAttack(enemyFactory.TeamOwner.Infantries, enemyFactory.TeamOwner.Hero);
                            if (enemyMilitary != null)
                            {
                                //_brainOwner.idle();
                                _brainOwner.attack(enemyMilitary);
                            }
                        }
                    }

                }
                else
                {
                    Military enemy = findEnemyInMyRangeForAttack();
                    if (enemy != null)
                        _brainOwner.attack(enemy);
                    else
                    {
                        Factory factory = findEnemyFactoryInRangeForAttack();
                        if (factory != null)
                            _brainOwner.attack(factory);
                    }
                }
            }
        }

        private Military findNearestEnemyInMyRangeForAttack()
        {
            Military enemy = null;
            float distance = 999999999;
            foreach (Team team in _teams)
            {
                if (team.TeamName != _brainOwner.TeamOwner.TeamName)
                {
                    foreach (Military military in team.Infantries)
                    {
                        float newDistance = Vector2.Distance(military.Position, _brainOwner.Position);
                        if (newDistance <= _brainOwner.RangeAttack && newDistance < distance)
                        {
                            if (!military.isDead())
                            {
                                distance = newDistance;
                                enemy = military;
                            }
                        }
                    }
                }
            }
            return enemy;        
        }

        public virtual Military findEnemyForAttack(List<Military> militaries, Military hero)
        {
            return null;
        }

        public virtual Military findEnemyForAttack()
        {
            return null;
        }

        public virtual Factory findFactoryForAttack()
        {
            return null;
        }

        private Military findEnemyInMyRangeForAttack()
        {
            Military enemy = null;
            foreach (Team team in _teams)
            {
                if (team.TeamName != _brainOwner.TeamOwner.TeamName)
                {
                    foreach (Military military in team.Infantries)
                    {
                        float newDistance = Vector2.Distance(military.Position, _brainOwner.Position);
                        if (newDistance <= _brainOwner.RangeAttack)
                        {
                            if (!military.isDead())
                            {
                                enemy = military;
                            }
                        }
                    }
                }
            }
            return enemy;
        }

        private Factory findEnemyFactoryInRangeForAttack()
        {
            Factory enemyFactory = null;
            foreach (Team team in _teams)
            {
                if (team.TeamName != _brainOwner.TeamOwner.TeamName)
                {
                    foreach (Factory factory in team.Factories)
                    {
                        float newDistance = Vector2.Distance(factory.Position, _brainOwner.Position);
                        if (newDistance <= _brainOwner.RangeAttack)
                        {
                            if (!factory.isDestroyed())
                            {
                                enemyFactory = factory;
                            }
                        }
                    }
                }
            }
            return enemyFactory;
        }
    }
}
